package demo;

import java.sql.*;

public class database {
    public static void main(String[] args) {

        try {
            // 1.注册驱动
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());

            // 获取数据库连接
            // serverTimezone=GMT 服务器时区
            // useSSL=false
            // characterEncoding=utf-8 字符集编码
            String url = "jdbc:mysql://localhost:3306/test2211?serverTimezone=GMT&useSSL=false&characterEncoding=utf-8";
            String user = "root";
            String password = "123456";
            Connection connection = DriverManager.getConnection(url, user, password);

            // 3.获取数据库操作对象
            Statement statement = connection.createStatement();
            String sql = "select * from tb_user";
            ResultSet resultSet = statement.executeQuery(sql);

            // 4.执行sql语句
           while (resultSet.next()) {

               // 查询
               int id = resultSet.getInt("id");
               String name = resultSet.getString("name");
               String address = resultSet.getString("address");
               System.out.println(id + "\t" + name + "\t" + address);

           }

            // 5.关闭数据库连接
            // 关闭资源时，要一个一个关，从小到大关
            statement.close();
            connection.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
